package com.softhinker.ke24.unit3.ui;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.table.TableModel;

import com.softhinker.ke24.unit3.model.Stock;

public class DetailsPanel extends JPanel implements MouseListener {

	private static final long serialVersionUID = 3697280323856158323L;

	private JLabel companyNameLabel = new JLabel("Company Name : ");
	private JLabel companyNameValue = new JLabel();
	private JLabel codeLabel = new JLabel("Code : ");
	private JLabel codeValue = new JLabel();
	private JLabel backgroundLabel = new JLabel("Background : ");
	private JTextArea backgroundValue = new JTextArea();
	private JScrollPane scrollPane = new JScrollPane(backgroundValue);
	
	public DetailsPanel(){
		this.setLayout(new SpringLayout());
		
		this.backgroundValue.setEditable(false);
		this.backgroundValue.setColumns(30);
		this.backgroundValue.setLineWrap(true);
		this.backgroundValue.setBackground(new Color(214, 217, 223));
		this.backgroundValue.setBorder(BorderFactory.createEmptyBorder());
		this.backgroundValue.setFocusable(false);
		
		add(companyNameLabel);
		add(companyNameValue);
		add(codeLabel);
		add(codeValue);
		add(backgroundLabel);
		add(scrollPane);
		
		SpringUtilities.makeCompactGrid(this, 3, 2, 1, 1, 1, 1);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		Object source = e.getSource();
		if(source instanceof JTable){
			JTable jtable = (JTable)source;
	        int row = jtable.getSelectedRow();
			TableModel model = jtable.getModel();
	        Object obj = model.getValueAt(row, 0);
	        Stock stock = (Stock)obj;
	        this.companyNameValue.setText(stock.getCounterName());
	        this.codeValue.setText(stock.getCode());
	        this.backgroundValue.setText(stock.getBackground());
		}
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
